package hk.m4s.lr.repair.test.ui.equipment;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.bumptech.glide.Glide;
import com.google.gson.stream.JsonReader;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;
import com.lib.shortvideo.RecordShortVideoActivity;
import com.lib.shortvideo.utils.FCCache;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.alertview.AlertView;
import framework.common.alertview.OnItemClickListener;
import framework.common.baseui.UeBaseActivity;
import framework.common.takephoto.CustomHelper;
import framework.common.widgets.dialogs.UeDialog;
import framework.common.zanetwork.core.ResponseCallback;
import framework.common.zanetwork.core.UploadCallback;
import framework.common.zanetwork.core.UploadHelper;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.MaintenanceInfoModel;
import hk.m4s.lr.repair.test.model.TwoCodeModel;
import hk.m4s.lr.repair.test.service.AccountSerive;
import hk.m4s.lr.repair.test.utils.FileUtils;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/5/5.
 * 维修任务详情已完成
 */

public class LookRepaireFinishDetailActivity extends UeBaseActivity implements OnItemClickListener{
    private Context mContext;
    private TextView bill_qcan_name,qcan_type,qcan_work,qcan_start_time,qcan_end_time;
    private TextView qcan_brand;
    private TextView qcan_stock;
    private ImageView qcan_img;
    private RelativeLayout click_repair,complain_click_area,complain_click_area_show;
    private LinearLayout showResult,endTimeLayout;
    private Button billing_sure;
    private TextView q_info;
    private TextView r_info;
    private String info="";
    private ImageView show_photo, img1, img2, img3,de_cacn;
    private String imgPath1 = "", imgPath2 = "", imgPath3 = "";
    private String imagLogo = "", imagLogo2 = "", imagLogo3 = "";
    // 用于保存图片资源文件
    private List<Bitmap> lists = new ArrayList<Bitmap>();
    private ArrayList<TImage> imgList;
    private int selectCode = 0;
    private int deletePotion = 0;
    // 拍照
    public static final int IMAGE_CAPTURE = 1;
    // 从相册选择
    public static final int IMAGE_SELECT = 2;
    // 照片缩小比例
    private static final int SCALE = 5;
    private String callback_url="";
    // 跳转到短视频界面的requestCode
    private static final int REQUEST_CODE_SHORT_VIDEO = 1001;
    // 跳转到短视频界面的resultCode
    private static final int RESULT_CODE_SHORT_VIDEO = 1002;
    private ImageView complain_status_btn, complain_add_video,complain_status_btn_show,complain_add_video_show;
    private ImageView showImg2,showImg3,showImg1;
    AlertView deAlertView;
    private  ProgressDialog progress;
    MaintenanceInfoModel model;
    private Handler showHandlers = null;
    private String videoUrl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_repaire_finish_detail);
        hiddenFooter();
        mContext=this;
        model=(MaintenanceInfoModel)getIntent().getSerializableExtra("model");
        qcan_img=(ImageView)findViewById(R.id.qcan_img);
        qcan_brand=(TextView)findViewById(R.id.qcan_brand);
        qcan_stock=(TextView)findViewById(R.id.qcan_stock);
        qcan_type=(TextView)findViewById(R.id.qcan_type);
        qcan_work=(TextView)findViewById(R.id.qcan_work);
        showResult=(LinearLayout) findViewById(R.id.showResult);
        endTimeLayout=(LinearLayout) findViewById(R.id.endTimeLayout);
        qcan_start_time=(TextView)findViewById(R.id.qcan_start_time);
        billing_sure=(Button)findViewById(R.id.billing_sure);
        bill_qcan_name=(TextView)findViewById(R.id.bill_qcan_name);
        q_info=(TextView) findViewById(R.id.q_info);
        r_info=(TextView) findViewById(R.id.r_info);
        qcan_end_time=(TextView) findViewById(R.id.qcan_end_time);
        show_photo = (ImageView) findViewById(R.id.show_photo);
        showImg1= (ImageView) findViewById(R.id.showImg1);
        showImg2= (ImageView) findViewById(R.id.showImg2);
        showImg3= (ImageView) findViewById(R.id.showImg3);
        img1 = (ImageView) findViewById(R.id.img1);
        img2 = (ImageView) findViewById(R.id.img2);
        img3 = (ImageView) findViewById(R.id.img3);

        complain_click_area_show= (RelativeLayout) findViewById(R.id.complain_click_area_show);
        click_repair = (RelativeLayout) findViewById(R.id.click_repair);
        complain_click_area= (RelativeLayout) findViewById(R.id.complain_click_area);
        complain_status_btn = (ImageView) findViewById(R.id.complain_status_btn);
        complain_add_video = (ImageView) findViewById(R.id.complain_add_video);
        complain_status_btn_show = (ImageView) findViewById(R.id.complain_status_btn_show);
        complain_add_video_show = (ImageView) findViewById(R.id.complain_add_video_show);
        setTitleText("设备维修");
        showHandlers=new Handler();
        showGoBackBtn();
        if(model!=null) {
            qcan_brand.setText(model.getBrandName());
            qcan_stock.setText(model.getUseClientName());
            bill_qcan_name.setText(model.getDeviceName());
            q_info.setText(model.getDescription());
            qcan_work.setText(model.getWorkbay());
            qcan_type.setText(model.getModel());
            if(model.getEndTime()==null||model.getEndTime().equals("")){
                endTimeLayout.setVisibility(View.GONE);
            }else{
                endTimeLayout.setVisibility(View.VISIBLE);
            }
            qcan_start_time.setText(model.getBeginTime());
            qcan_end_time.setText(model.getEndTime());
            r_info.setText(model.getInfo());
            if(model.getDeviceLogo()!=null) {
                Glide.with(mContext).load(model.getDeviceLogo()).asBitmap().error(R.mipmap.device_list_no_img).into(qcan_img);
            }
            if(model.getImgs()==null||model.getImgs().equals("")){
                showImg1.setVisibility(View.GONE);
                showImg2.setVisibility(View.GONE);
                showImg3.setVisibility(View.GONE);
            }else{
                try {
                    List<String> list=new ArrayList<String>();
                    JsonReader jr=new JsonReader(new StringReader(model.getImgs()));//tname为json字符串
                        jr.beginArray();// 开始解析数组
                        while (jr.hasNext()) {
                            jr.beginObject();// 开始解析对象
                            while (jr.hasNext()) {
                                if (jr.nextName().equals("imgUrl")) {
                                    // 解析键值
                                    list.add(jr.nextString());
                                }

                            }
                            jr.endObject();// 解析对象结束
                        }
                        jr.endArray();// 解析数组结束
                    if(list.size()>0){
                        if(list.size()==1){
                            showImg1.setVisibility(View.VISIBLE);
                            showImg2.setVisibility(View.GONE);
                            showImg3.setVisibility(View.GONE);
                            Glide.with(mContext).load(list.get(0)).asBitmap().error(R.mipmap.device_list_no_img).into(showImg1);
                        }else if(list.size()==2){
                            showImg1.setVisibility(View.VISIBLE);
                            showImg2.setVisibility(View.VISIBLE);
                            showImg3.setVisibility(View.GONE);
                            Glide.with(mContext).load(list.get(0)).asBitmap().error(R.mipmap.device_list_no_img).into(showImg1);
                            Glide.with(mContext).load(list.get(1)).asBitmap().error(R.mipmap.device_list_no_img).into(showImg2);
                        }else if(list.size()==3){
                            showImg1.setVisibility(View.VISIBLE);
                            showImg2.setVisibility(View.VISIBLE);
                            showImg3.setVisibility(View.VISIBLE);
                            Glide.with(mContext).load(list.get(0)).asBitmap().error(R.mipmap.device_list_no_img).into(showImg1);
                            Glide.with(mContext).load(list.get(1)).asBitmap().error(R.mipmap.device_list_no_img).into(showImg2);
                            Glide.with(mContext).load(list.get(2)).asBitmap().error(R.mipmap.device_list_no_img).into(showImg3);
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(model.getImg()==null||model.getImg().equals("")){
                img1.setVisibility(View.GONE);
                img2.setVisibility(View.GONE);
                img3.setVisibility(View.GONE);
            }else{
                try {
                    List<String> list=new ArrayList<String>();
                    JsonReader jr=new JsonReader(new StringReader(model.getImg()));//tname为json字符串
                    jr.beginArray();// 开始解析数组
                    while (jr.hasNext()) {
                        jr.beginObject();// 开始解析对象
                        while (jr.hasNext()) {
                            if (jr.nextName().equals("imgUrl")) {
                                // 解析键值
                                list.add(jr.nextString());
                            }

                        }
                        jr.endObject();// 解析对象结束
                    }
                    jr.endArray();// 解析数组结束
                    if(list.size()>0){
                        if(list.size()==1){
                            img1.setVisibility(View.VISIBLE);
                            img2.setVisibility(View.GONE);
                            img3.setVisibility(View.GONE);
                            Glide.with(mContext).load(list.get(0)).asBitmap().error(R.mipmap.device_list_no_img).into(img1);
                        }else if(list.size()==2){
                            img1.setVisibility(View.VISIBLE);
                            img2.setVisibility(View.VISIBLE);
                            img3.setVisibility(View.GONE);
                            Glide.with(mContext).load(list.get(0)).asBitmap().error(R.mipmap.device_list_no_img).into(img1);
                            Glide.with(mContext).load(list.get(1)).asBitmap().error(R.mipmap.device_list_no_img).into(img2);
                        }else if(list.size()==3){
                            img1.setVisibility(View.VISIBLE);
                            img2.setVisibility(View.VISIBLE);
                            img3.setVisibility(View.VISIBLE);
                            Glide.with(mContext).load(list.get(0)).asBitmap().error(R.mipmap.device_list_no_img).into(img1);
                            Glide.with(mContext).load(list.get(1)).asBitmap().error(R.mipmap.device_list_no_img).into(img2);
                            Glide.with(mContext).load(list.get(2)).asBitmap().error(R.mipmap.device_list_no_img).into(img3);
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(model.getVideo()==null||model.getVideo().equals("")){
                complain_click_area_show.setVisibility(View.GONE);
            }else{
                videoUrl=model.getVideo();
                complain_click_area_show.setVisibility(View.VISIBLE);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                            showHandlers.post(runnableUi);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
    }
    public void onClick(View view){
        switch (view.getId()){
            case R.id.complain_click_area:
                if (!(callback_url == null || callback_url.equals(""))) {
                    Intent intents = new Intent(Intent.ACTION_VIEW);
                    if (intents != null) {
                        String type = "video/* ";
                        Uri uri = Uri.parse(callback_url);
                        intents.setDataAndType(uri, type);
                        startActivity(intents);
                    }
                } else {
                    RecordShortVideoActivity.start(this, 10, 1, 1.0f, FCCache
                                    .getInstance().getVideoCachePath(),
                            REQUEST_CODE_SHORT_VIDEO, RESULT_CODE_SHORT_VIDEO);
                }
                break;
        }
    }
    @Override
    public void onItemClick(Object o, int position) {
        //判断是否是拓展窗口View，而且点击的是非取消按钮
        if (position == -1) {
            return;
        } else {
            if (selectCode == 0) {
                if (position == 1) {
                    CustomHelper.onClick(getTakePhoto(), position);
                } else if (position == 0) {
                    CustomHelper.onClick(getTakePhoto(), position);
                }
            } else if (selectCode == 2) {
                if (deletePotion == 1) {
                    imgPath1 = "";
                    imagLogo="";
                    img1.setVisibility(View.GONE);
                    show_photo.setVisibility(View.VISIBLE);
                } else if (deletePotion == 2) {
                    imgPath2 = "";
                    imagLogo2="";
                    img2.setVisibility(View.GONE);
                    show_photo.setVisibility(View.VISIBLE);
                } else if (deletePotion == 3) {
                    imgPath3 = "";
                    imagLogo3="";
                    img3.setVisibility(View.GONE);
                    show_photo.setVisibility(View.VISIBLE);
                }
            }

        }
    }
    Runnable runnableUi = new Runnable() {
        @Override
        public void run() {
            //更新界面
            if (!(videoUrl == null || videoUrl.equals(""))) {
                complain_click_area_show.setVisibility(View.VISIBLE);
                Bitmap bimp = FileUtils
                        .createVideoThumbnail(videoUrl, 80,
                                80);
                if (bimp != null) {
                    complain_add_video_show
                            .setVisibility(View.VISIBLE);
                    complain_status_btn_show.setVisibility(View.VISIBLE);
                    complain_add_video_show
                            .setImageBitmap(bimp);
                }

            } else {
                complain_click_area_show.setVisibility(View.GONE);
            }

        }

    };
}
